package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.PageUser;
import com.itheima.dto.SysUserDTO;
import com.itheima.entity.SysUser;
import com.itheima.mapper.SysUserMapper;
import com.itheima.result.PageData;
import com.itheima.service.SysUserService;
import com.itheima.utils.IpUtils;
import com.itheima.vo.SysUserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @projectName: kafka
 * @package: com.itheima.service.impl
 * @className: SysUserServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/23 14:51
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {
    private final SysUserMapper sysUserMapper;
    private final IpUtils ipUtils;

    @Override
    public PageData page(PageUser pageUser) {
        PageHelper.startPage(pageUser.getPage(), pageUser.getLimit());
        List<SysUser> sysUserList = sysUserMapper.getAllUser(pageUser.getLogin_name(),pageUser.getStatus(),pageUser.getPhone());
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUserList);

        List<SysUserVO> sysUserVOList = new ArrayList<>();
        for (SysUser sysUser : sysUserList) {
            SysUserVO sysUserVO = new SysUserVO();
            BeanUtils.copyProperties(sysUser,sysUserVO);
            sysUserVO.setLogin_name(sysUser.getLoginName());
            sysUserVO.setLogin_ip(sysUser.getLoginIp());
            sysUserVO.setLogin_date(sysUser.getLoginDate());
            sysUserVO.setCreate_date(sysUser.getCreateDate());
            sysUserVO.setUpdate_date(sysUser.getUpdateDate());
            sysUserVO.setDel_flag(sysUser.getDelFlag());
            sysUserVO.setDefault_data(sysUser.getDefaultData());
            List<String> roleId = JSON.parseArray(sysUser.getRoleId(), String.class);
            sysUserVO.setRole_id(roleId);
            List<String> roleName = sysUserMapper.selectRoleNameByIds(roleId);
            sysUserVO.setRolename(roleName);
            sysUserVOList.add(sysUserVO);
        }
        return new PageData(pageInfo.getTotal(),
                pageInfo.getPages(),
                pageInfo.getPageSize(),
                pageInfo.getPageNum(),
                sysUserVOList);
    }

    @Override
    public void creatUser(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO,sysUser);
        sysUser.setId(UUID.randomUUID().toString());
        sysUser.setLoginName(sysUserDTO.getLogin_name());
        sysUser.setDelFlag("0");
        sysUser.setStatus(1);
        List<String> role_id = sysUserDTO.getRole_id();
        // String joinString = String.join(",", sysUserDTO.getRole_id());
        // System.out.println("---------------joinString--------------"+joinString);   "151afaw11","fwaf151"  这种是错误的形式
        // 得转换成 ["151afaw11","fwaf151"]
        // JSON.toJSONString()   List -- > []  存储到数据库中text类型
        sysUser.setRoleId(JSON.toJSONString(role_id));
        sysUserMapper.creatUser(sysUser);
    }

    @Override
    public void deleteUser(String id) {
        sysUserMapper.deleteUser(id);
    }

    @Override
    public SysUserVO queryUserInfo(String id) {
        SysUser sysUser = sysUserMapper.selectUserById(id);
        SysUserVO sysUserVO = new SysUserVO();
        BeanUtils.copyProperties(sysUser,sysUserVO);
        sysUserVO.setLogin_name(sysUser.getLoginName());
        sysUserVO.setLogin_ip(sysUser.getLoginIp());
        sysUserVO.setLogin_date(sysUser.getLoginDate());
        sysUserVO.setCreate_date(sysUser.getCreateDate());
        sysUserVO.setUpdate_date(sysUser.getUpdateDate());
        sysUserVO.setDel_flag(sysUser.getDelFlag());
        sysUserVO.setDefault_data(sysUser.getDefaultData());
        List<String> roleId = JSON.parseArray(sysUser.getRoleId(), String.class);
        sysUserVO.setRole_id(roleId);
        return sysUserVO;
    }

    @Override
    public void updateUser(SysUserDTO sysUserDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDTO,sysUser);
        sysUser.setLoginName(sysUserDTO.getLogin_name());
        sysUser.setLoginIp(ipUtils.getClientIp());
        sysUser.setLoginDate(LocalDateTime.now());
        String roleId = JSON.toJSONString(sysUserDTO.getRole_id());
        sysUser.setRoleId(roleId);
        sysUserMapper.updateUser(sysUser);
    }
}
